package com.bingxue.edu.parent.service;

import cn.hutool.core.util.ArrayUtil;
import com.bingxue.edu.common.enums.StudentClassEnum;
import com.bingxue.edu.management.education.service.LessonStudentService;
import com.bingxue.edu.parent.model.query.StudentScheduleLessonQuery;
import com.bingxue.edu.parent.model.resp.StudentScheduleLessonResp;
import com.bingxue.edu.parent.model.resp.StudentScheduleLessonRespLegacy;
import com.mybatisflex.core.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.bingxue.edu.management.education.model.entity.table.ClassInfoTableDef.CLASS_INFO;
import static com.bingxue.edu.management.education.model.entity.table.ClassroomTableDef.CLASSROOM;
import static com.bingxue.edu.management.education.model.entity.table.LessonStudentTableDef.LESSON_STUDENT;
import static com.bingxue.edu.management.education.model.entity.table.LessonTableDef.LESSON;
import static com.bingxue.edu.management.system.model.entity.table.OrgTableDef.ORG;

@Slf4j
@Service
@RequiredArgsConstructor
public class StudentScheduleService {

    private final LessonStudentService lessonStudentService;


    public List<StudentScheduleLessonRespLegacy> getScheduleLessonListLegacy(StudentScheduleLessonQuery query) {
        QueryWrapper queryWrapper = QueryWrapper.create()
                .select(
                        ArrayUtil.addAll(LESSON.DEFAULT_COLUMNS, LESSON_STUDENT.DEFAULT_COLUMNS),
                        CLASS_INFO.NAME.as(StudentScheduleLessonResp::getClassName),
                        ORG.NAME.as(StudentScheduleLessonResp::getCampusName),
                        CLASSROOM.NAME.as(StudentScheduleLessonResp::getClassroomName)
                )
                .from(LESSON_STUDENT)
                .leftJoin(LESSON).on(LESSON_STUDENT.LESSON_ID.eq(LESSON.ID))
                .leftJoin(CLASS_INFO).on(LESSON.CLASS_ID.eq(CLASS_INFO.ID))
                .leftJoin(CLASSROOM).on(LESSON.CLASSROOM_ID.eq(CLASSROOM.ID))
                .leftJoin(ORG).on(LESSON.CAMPUS_ID.eq(ORG.ID))
                .where(LESSON_STUDENT.STUDENT_CLASS_STATUS.eq(StudentClassEnum.NORMAL)) // 正常
                    .and(LESSON_STUDENT.STUDENT_ID.eq(query.getStudentId()))
                    .and(LESSON.LESSON_DATE.ge(query.getLessonDateFrom()))
                    .and(LESSON.LESSON_DATE.lt(query.getLessonDateTo().plusDays(1)));
        return lessonStudentService.listAs(queryWrapper, StudentScheduleLessonRespLegacy.class);
    }

    public List<StudentScheduleLessonResp> getScheduleLessonList(StudentScheduleLessonQuery query) {
        QueryWrapper queryWrapper = QueryWrapper.create()
                .select(
                        LESSON.DEFAULT_COLUMNS,
                        LESSON_STUDENT.DEFAULT_COLUMNS
                )
                .from(LESSON_STUDENT)
                    .leftJoin(LESSON).on(LESSON_STUDENT.LESSON_ID.eq(LESSON.ID))
                .where(LESSON_STUDENT.STUDENT_CLASS_STATUS.eq(StudentClassEnum.NORMAL)) // 正常
                    .and(LESSON_STUDENT.STUDENT_ID.eq(query.getStudentId()))
                    .and(LESSON.LESSON_DATE.ge(query.getLessonDateFrom()))
                    .and(LESSON.LESSON_DATE.le(query.getLessonDateTo()));
        return lessonStudentService.getMapper().selectListWithRelationsByQueryAs(queryWrapper, StudentScheduleLessonResp.class);
    }

}
